package com.ym.controller;

import com.github.pagehelper.PageInfo;
import com.ym.common.annotations.CustomConstraint;
import com.ym.common.constant.Constants;
import com.ym.common.utils.page.PageDomain;
import com.ym.common.utils.sensitive.SensitiveFilter;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.medical.domain.entity.SubjectEntity;
import com.ym.medical.domain.entity.SubjectReplyEntity;
import com.ym.medical.domain.param.SubjectQueryParam;
import com.ym.medical.domain.param.SubjectReplyUpdateMobileParam;
import com.ym.medical.domain.param.SubjectUpdateMobileParam;
import com.ym.medical.domain.vo.SubjectAnswerVo;
import com.ym.medical.domain.vo.SubjectListMobileVO;
import com.ym.medical.domain.vo.SubjectReplyListMobileVO;
import com.ym.medical.enums.CommentTypeEnum;
import com.ym.medical.service.ISubjectReplySV;
import com.ym.medical.service.ISubjectSV;
import com.ym.medical.service.IUserLikeRecordSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

import static com.ym.core.shiro.ShiroUtil.getLoginId;

/**
 * 主题Controller
 *
 * @author ym
 * @date 2020-08-24
 */
@Api(tags = "mobile-评论相关")
@RestController
@Validated
@RequestMapping(value = "/subject", headers = Constants.Api.VERSION)
public class SubjectController extends BaseController {


	@Autowired
	private ISubjectSV subjectSV;

	@Autowired
	private ISubjectReplySV subjectReplySV;

	@Autowired
	private IUserLikeRecordSV userLikeRecordSV;


	@Log
	@ApiOperation("主题查询")
	@GetMapping("/search")
	@ResponseBody
	public APIResponse<SubjectAnswerVo> search(SubjectQueryParam queryParam) {
		SubjectAnswerVo subjectAnswerVo = subjectSV.search(queryParam);
		return successData(subjectAnswerVo);
	}

	@Log
	@ApiOperation("主题列表")
	@GetMapping("/searchList")
	@ResponseBody
	public APIResponse<PageInfo<SubjectAnswerVo>> searchList(SubjectQueryParam queryParam) {
		PageDomain.startPage();
		List<SubjectAnswerVo> subjectAnswerVo = subjectSV.searchList(queryParam);
		return successPage(subjectAnswerVo);
	}

	@Log
	@ApiOperation("评论详情")
	@GetMapping("/list")
	@ResponseBody
	public APIResponse<PageInfo<SubjectListMobileVO>> listPageSubject(SubjectQueryParam queryParam) {
		PageInfo<SubjectListMobileVO> subjectVoList = subjectSV.getSubjectDetail(queryParam.getAnswerType(), queryParam.getAnswerId(), getLoginId(true));
		return successData(subjectVoList);
	}

	@Log
	@ApiOperation("主题评论")
	@PostMapping("/comment")
	@ResponseBody
	public APIResponse commentSubject(
			@ApiParam(name = "subject", value = "评论信息", required = true)
			@RequestBody SubjectUpdateMobileParam subjectEditParam) {

		if (SensitiveFilter.MY_SQL.contain(subjectEditParam.getContent())) {
			return failMsg("内容包含敏感词汇,请重新编辑发表");
		}
		SubjectEntity subjectEntity = subjectEditParam.covert();
		subjectEntity.setUserId(getLoginId(true));
		subjectEntity.setCrtSb(getLoginId(true));
		subjectSV.insert(subjectEntity);
		return successMsg("主题评论成功");
	}

	@Log
	@ApiOperation("主题答复")
	@PostMapping("/reply")
	@ResponseBody
	public APIResponse replySubject(
			@ApiParam(name = "subject", value = "评论信息", required = true)
			@RequestBody SubjectReplyUpdateMobileParam subjectReplyEditParam) {
		if (SensitiveFilter.MY_SQL.contain(subjectReplyEditParam.getContent())) {
			return failMsg("内容包含敏感词汇,请重新编辑发表");
		}
		SubjectReplyEntity subjectReply = subjectReplyEditParam.covert();
		subjectReply.setUserId(getLoginId());
		subjectReplySV.insert(subjectReply);
		return successMsg("主题答复成功");
	}

	@ApiOperation("删除主题")
	@Log
	@PostMapping("/deleteSubject")
	public APIResponse delete(@RequestBody @Valid @NotNull Long[] ids) {
		int result = subjectSV.deleteByIds(ids);
		return successData(result);
	}

	@ApiOperation("主题答复删除")
	@Log
	@PostMapping("/deleteSubjectReply")
	public APIResponse deleteSubjectReply(@RequestBody @Valid @NotNull Long[] ids) {
		int result = subjectReplySV.deleteByIds(ids);
		return successData(result);
	}

	@Log
	@ApiOperation("根据问题id查询回复")
	@GetMapping("/listReply")
	@ResponseBody
	public APIResponse<List<SubjectReplyListMobileVO>> listReply(
			@ApiParam(name = "answerId", value = "问题id", required = true)
			@RequestParam Long answerId) {
		List<SubjectReplyListMobileVO> subjectReplys = subjectReplySV.getSubjectReplyDetail(answerId, getLoginId());
		return successData(subjectReplys);
	}


	@Log
	@ApiOperation("点赞")
	@GetMapping("/doLike")
	@ResponseBody
	public APIResponse doLike(
			@ApiParam(name = "id", value = "关联主键", required = true)
			@RequestParam Long id,
			@ApiParam(name = "commentType", value = "评论类型 (CommentTypeEnum)", required = true)
			@CustomConstraint(CommentTypeEnum.class)
			@RequestParam String commentType
	) {
		CommentTypeEnum commentTypeEnum = CommentTypeEnum.valueOf(CommentTypeEnum.class, commentType);
		userLikeRecordSV.doLike(getLoginId(), id, commentTypeEnum);
		return success();
	}

}
